home *** CD-ROM | disk | FTP | other *** search
/ The X-Philes (2nd Revision) / The X-Philes Number 1 (1995).iso / xphiles / hp48_1 / new2q.wic < prev    next >
Internet Message Format  |  1995-03-23  |  5KB

  1. From comp.sys.handhelds Thu Apr  4 13:04:21 1991
  2. Path: uncw!ecsvax!ecsgate!mcnc!uvaarpa!haven!uflorida!caen!sdd.hp.com!hp-pcd!hpcvra.cv.hp.com!billw
  3. From: billw@hpcvra.cv.hp.com. (William C Wickes)
  4. Newsgroups: comp.sys.handhelds
  5. Subject: ->Q or Not ->Q
  6. Message-ID: <25590130@hpcvra.cv.hp.com.>
  7. Date: 3 Apr 91 01:07:32 GMT
  8. Organization: Hewlett-Packard Co., Corvallis, OR, USA
  9. Lines: 108
  10.  
  11. From the HP 48 Design Team:
  12.  
  13.  
  14.                ->Q OR NOT ->Q, THAT IS THE QUESTION
  15.  
  16. Thanks to Joseph Horn, the HP28/48 community now has additional fast
  17. rationalization functionality for their machines (see "HP 48 Improved
  18. ->Q".)  We congratulate him for this elegant solution!  There are a
  19. number of interesting points regarding DEC2FRACs relation to ->Q
  20. which deserve some mention.  Perhaps these will stimulate discussion
  21. and further contributions.
  22.  
  23. DEC2FRAC is an addition and not a replacement for ->Q because its aim
  24. is rather different. DEC2FRAC's aim is to produce the fraction with
  25. the _smallest error_ and a given denominator size. ->Q's aim is to
  26. produce the fraction with the _smallest denominator_ and a given error
  27. size. This is best illustrated with the golden mean, '(1+\sqr(5))/2'.
  28. Given this and 1E11, DEC2FRAC returns '139583862445/86267571272' while
  29. ->Q returns (in STD mode) '514229/317811'. When evaluated, these
  30. return _exactly_ the same floating point number. The idea of ->Q is
  31. to "round to simpler" rather than "round to closer." This is useful
  32. where you have what you believe to be a "simple" fraction contaminated
  33. with roundoff or other noise.
  34.  
  35. The HP48 manuals did little to clarify this issue, especially since we
  36. wanted to make no specific claim with only a conjecture of correctness. In
  37. this regard, posting a proof that the algorithm fills in all possible
  38. fractions would be a great boon to the community.
  39.  
  40. While for many "interesting" numbers the continued fraction sequence as 
  41. generated by DUP IP SWAP FP INV ... is exact, even though the floating 
  42. point representation is not, in other cases, you get "noise" unexpectedly 
  43. early. Does this have any significance regarding the "safety" of generating
  44. the numerator of the fraction from the denominator by division?
  45.  
  46. While ->Q keeps around the entire continued fraction sequence entailing
  47. some speed penalty, there may be other statistics about the sequence
  48. (such as periodicity) which can be of interest in "deciphering" a
  49. floating point number. Suggestions along these lines are most welcome.
  50.  
  51. Thanks again to J. Horn for a stimulating and useful post.
  52.  
  53.  
  54. Included below is a program, NEW2Q which follows Horn's algorithm, but
  55. uses exit conditions like those of ->Q.
  56.  
  57. @ NEW2Q: Version of ->Q based on J.K.Horn's Algorithm
  58. @
  59. @ Input:
  60. @
  61. @ 2: Decimal Number to be converted to a fraction
  62. @ 1: Number of decimal places of zeros required in the error.
  63. @
  64. @ Output:
  65. @
  66. @ 1: 'Numerator/Denominator'
  67. @
  68. @ Example:
  69. @ What's the simplest fraction which agrees with sqrt(3) to 4 decimal places?
  70. @   3 \sqr 4 NEQ2Q returns '97/56'
  71. @   '97/56-\sqr(3)' EVAL returns .00009294957
  72. @                                 ^^^^  note 4 zeros.
  73. @
  74. @ Checksum and bytes:
  75. @   #3992d
  76. @   620.5
  77.  
  78. %%HP: T(3)A(R)F(.);
  79. \<< DUP2
  80.   IF 1 > SWAP FP AND
  81.   THEN OVER XPON 1 -                        @ calculate the input exponent.
  82.     \<< \-> X 'IFTE(X==0,-500,XPON(X))' \>> @ define a 0-tolerant XPON.
  83.   \-> f c x expo
  84.     \<< 0 1 1 f DUP IP SWAP FP              @ set recursion initial cond.s.
  85.       WHILE 
  86.        OVER 5 PICK / f - ABS expo EVAL      @ calculate expon. of error
  87.        x SWAP - c <                         @ and compare with input.
  88.        OVER AND                             @ if not close enough and
  89.                                             @ the remainder's non-zero
  90.       REPEAT 
  91.        INV DUP FP SWAP IP                   @ then calculate next iterate.
  92.        \-> B0 B1 A0 A1 R B
  93.         \<< B1 'B*B1+B0' EVAL 
  94.             A1 'B*A1+A0' EVAL 
  95.             R
  96.         \>>
  97.       END 
  98.       DROP SWAP DROP SWAP 
  99.       DUP 4 ROLL - DUP f * 0 RND            @ calc. "missing" den. and num.
  100.       \-> N D D0 N0
  101.       \<<
  102.         IF 'x-expo(ABS(f-N0/D0))<c'         @ if "missing" frac. is not
  103.         THEN N D                            @ good enough, use original.
  104.         ELSE N0 D0
  105.           IF 'N0\=/N'                       @ If it is really new,
  106.           THEN 200 .2 BEEP                  @ then beep.
  107.           END
  108.         END
  109.       \>>
  110.     \>>                                     @ We're done, now clean up.
  111.     IF DUP ABS 1 >   
  112.     THEN # 352318d SYSEVAL
  113.     ELSE DROP
  114.     END
  115.   ELSE DROP
  116.   END
  117. \>>
  118.  
  119.